﻿namespace QMVnSoft.DAL
{
    using System;
    using System.Data;
    using System.Data.SqlClient;

    public class DmHdDataAccess
    {
        public bool DeleteRowDetail(SqlConnection po_Connection, SqlTransaction po_Transaction, object po_KeyCode)
        {
            bool flag;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt0", po_KeyCode), new SqlParameter() };
            values[1].Direction = ParameterDirection.ReturnValue;
            values[1].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmHd0_Delete";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                flag = ((int) values[1].Value) == 1;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression: ", exception);
            }
            return flag;
        }

        public DataSet GetDataSet()
        {
            DataSet set2;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            DataSet dataSet = new DataSet();
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmHd_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                adapter.SelectCommand = command;
                adapter.Fill(dataSet);
                AppDataAccess.CloseConnection(eZConnection);
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return set2;
        }

        public DataSet GetDataSet(string ps_Code)
        {
            DataSet set2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Ma_Hd", ps_Code) };
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            DataSet dataSet = new DataSet();
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmHd_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataSet);
                AppDataAccess.CloseConnection(eZConnection);
                set2 = dataSet;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return set2;
        }

        public DataTable GetDetailTable(string ps_Stt)
        {
            DataTable table2;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt", ps_Stt) };
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            DataTable dataTable = new DataTable();
            SqlDataAdapter adapter = new SqlDataAdapter();
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmHd0_Get";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                adapter.SelectCommand = command;
                adapter.Fill(dataTable);
                AppDataAccess.CloseConnection(eZConnection);
                table2 = dataTable;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return table2;
        }

        public bool SaveDetailTable(object ps_KeyCode, DataTable po_DetailDataTable)
        {
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlTransaction transaction = eZConnection.BeginTransaction();
            bool flag = true;
            foreach (DataRow row in po_DetailDataTable.Rows)
            {
                row.EndEdit();
                if ((row.RowState != DataRowState.Deleted) && ((((decimal) row["So_Luong"]) != 0M) || (((decimal) row["Tien"]) != 0M)))
                {
                    if (((row.RowState != DataRowState.Added) && (row.RowState != DataRowState.Modified)) || (this.SaveRowDetail(eZConnection, transaction, ps_KeyCode, row) == 1))
                    {
                        continue;
                    }
                    flag = false;
                    break;
                }
                if (row.RowState == DataRowState.Deleted)
                {
                    this.DeleteRowDetail(eZConnection, transaction, row["Stt3", DataRowVersion.Original]);
                }
                else
                {
                    this.DeleteRowDetail(eZConnection, transaction, row["Stt3"]);
                }
            }
            if (flag)
            {
                transaction.Commit();
                AppDataAccess.CloseConnection(eZConnection);
                return true;
            }
            transaction.Rollback();
            AppDataAccess.CloseConnection(eZConnection);
            return false;
        }

        public int SaveRow(bool pb_NewChecked, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { 
                new SqlParameter("p_Moi_Sua", pb_NewChecked), new SqlParameter("p_Stt", row["Stt"]), new SqlParameter("p_Ma_HD", row["Ma_HD"]), new SqlParameter("p_Ten_HD", row["Ten_HD"]), new SqlParameter("p_Ten_HD_E", row["Ten_HD_E"]), new SqlParameter("p_Loai_HD", row["Loai_HD"]), new SqlParameter("p_Tk", row["Tk"]), new SqlParameter("p_Ma_Dt", row["Ma_Dt"]), new SqlParameter("p_Ngay_Ky", row["Ngay_Ky"]), new SqlParameter("p_TTien_Nt0", row["TTien_Nt0"]), new SqlParameter("p_TTien0", row["TTien0"]), new SqlParameter("p_TTien_Nt", row["TTien_Nt"]), new SqlParameter("p_TTien", row["TTien"]), new SqlParameter("p_PT_Lai", row["PT_Lai"]), new SqlParameter("p_PT_Lai1", row["PT_Lai1"]), new SqlParameter("p_Ngay_HD1", row["Ngay_HD1"]), 
                new SqlParameter("p_Ngay_HD2", row["Ngay_HD2"]), new SqlParameter("p_Ma_Loai1", row["Ma_Loai1"]), new SqlParameter("p_Ma_Loai2", row["Ma_Loai2"]), new SqlParameter("p_Ma_Loai3", row["Ma_Loai3"]), new SqlParameter("p_Result", SqlDbType.BigInt)
             };
            values[20].Direction = ParameterDirection.Output;
            values[20].SqlDbType = SqlDbType.Int;
            SqlConnection eZConnection = AppDataAccess.AppConnection;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmHd_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = eZConnection;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                AppDataAccess.CloseConnection(eZConnection);
                num = (int) values[20].Value;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }

        public int SaveRowDetail(SqlConnection po_Connection, SqlTransaction po_Transaction, object po_MasterKey, DataRow row)
        {
            int num;
            SqlParameter[] values = new SqlParameter[] { new SqlParameter("p_Stt0", row["Stt0"]), new SqlParameter("p_Stt", po_MasterKey), new SqlParameter("p_Ma_Vt", row["Ma_Vt"]), new SqlParameter("p_So_Luong", row["So_Luong"]), new SqlParameter("p_Gia", row["Gia"]), new SqlParameter("p_Tien", row["Tien"]), new SqlParameter("p_Result", SqlDbType.BigInt) };
            values[6].Direction = ParameterDirection.Output;
            values[6].SqlDbType = SqlDbType.Int;
            SqlCommand command = new SqlCommand();
            try
            {
                command.CommandText = "DmHd0_Save";
                command.CommandType = CommandType.StoredProcedure;
                command.Connection = po_Connection;
                command.Transaction = po_Transaction;
                command.Parameters.AddRange(values);
                command.ExecuteNonQuery();
                num = (int) values[6].Value;
            }
            catch (Exception exception)
            {
                throw new NotSupportedException("Invalid expression", exception);
            }
            return num;
        }
    }
}

